#include "mylib.h"



void data_deal(char *data, int data_len, void *ptr)
{
	//printf("recv data\n");
	char buff[500];
	memcpy(buff, data, data_len);
	net_epoll_send_date((PS_TASK_NET)ptr, NULL, buff, data_len);
	return;
}
int main(int argc, char **argv)
{
	PS_TASK_NET pstNetS1, pstNetS2, pstNetC1, pstNetC2;
	PS_TASK_NET pstNetU1, pstNetU2, pstNetU3, pstNetU4;
	struct sockaddr_in addr;

	net_epoll_init(5, 10);
	bzero(&addr, sizeof(struct sockaddr_in));
	addr.sin_family = AF_INET;
	addr.sin_port = htons(10000);
	addr.sin_addr.s_addr = inet_addr("192.168.13.136");
	pstNetC1 = net_epoll_add_task(NET_TASK_CMD_ADD_CONNECT_TCP, (struct sockaddr *)&addr, data_deal, 500);
	bzero(&addr, sizeof(struct sockaddr_in));
	addr.sin_family = AF_INET;
	addr.sin_port = htons(10000);
	addr.sin_addr.s_addr = inet_addr("192.168.13.172");
	pstNetC2 = net_epoll_add_task(NET_TASK_CMD_ADD_CONNECT_TCP, (struct sockaddr *)&addr, data_deal, 500);
	//pstNet = net_epoll_add_task(NET_TASK_CMD_ADD_CONNECT_TCP, (struct sockaddr *)&addr, data_deal, 500);
	bzero(&addr, sizeof(struct sockaddr_in));
	addr.sin_family = AF_INET;
	addr.sin_port = htons(12000);
	pstNetS1 = net_epoll_add_task(NET_TASK_CMD_ADD_LISTENING_TCP, (struct sockaddr *)&addr, data_deal, 100);
	bzero(&addr, sizeof(struct sockaddr_in));
	addr.sin_family = AF_INET;
	addr.sin_port = htons(12001);
	pstNetS2 = net_epoll_add_task(NET_TASK_CMD_ADD_LISTENING_TCP, (struct sockaddr *)&addr, data_deal, 100);
	bzero(&addr, sizeof(struct sockaddr_in));
	addr.sin_family = AF_INET;
	addr.sin_port = htons(13000);
	pstNetU1 = net_epoll_add_task(NET_TASK_CMD_ADD_UDP, (struct sockaddr *)&addr, data_deal, 100);
	bzero(&addr, sizeof(struct sockaddr_in));
	addr.sin_family = AF_INET;
	addr.sin_port = htons(13001);
	pstNetU2 = net_epoll_add_task(NET_TASK_CMD_ADD_UDP, (struct sockaddr *)&addr, data_deal, 100);
	bzero(&addr, sizeof(struct sockaddr_in));
	addr.sin_family = AF_INET;
	addr.sin_port = htons(13002);
	pstNetU3 = net_epoll_add_task(NET_TASK_CMD_ADD_UDP, (struct sockaddr *)&addr, data_deal, 100);
	bzero(&addr, sizeof(struct sockaddr_in));
	addr.sin_family = AF_INET;
	addr.sin_port = htons(13003);
	pstNetU4 = net_epoll_add_task(NET_TASK_CMD_ADD_UDP, (struct sockaddr *)&addr, data_deal, 100);
	while(1)
	{
		if (!net_epoll_connect_is_health(pstNetC1))
		{
			net_epoll_del_task(pstNetC1);
			bzero(&addr, sizeof(struct sockaddr_in));
			addr.sin_family = AF_INET;
			addr.sin_port = htons(10000);
			addr.sin_addr.s_addr = inet_addr("192.168.13.136");
			pstNetC1 = net_epoll_add_task(NET_TASK_CMD_ADD_CONNECT_TCP, (struct sockaddr *)&addr, data_deal, 500);
		}

		if (!net_epoll_connect_is_health(pstNetC2))
		{
			net_epoll_del_task(pstNetC2);
			bzero(&addr, sizeof(struct sockaddr_in));
			addr.sin_family = AF_INET;
			addr.sin_port = htons(10000);
			addr.sin_addr.s_addr = inet_addr("192.168.13.172");
			pstNetC2 = net_epoll_add_task(NET_TASK_CMD_ADD_CONNECT_TCP, (struct sockaddr *)&addr, data_deal, 500);
		}

		sleep(5);
	}
	return 0;
}
